package temp;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.CharacterRun;
import org.apache.poi.hwpf.usermodel.Paragraph;

import java.io.FileInputStream;

public class WordTiQu {
    //    public static void main(String[] args) throws IOException {
//        // 读取word文档
//        FileInputStream fis = new FileInputStream("C:\\Users\\EDY\\Desktop\\模版信息\\中期文档\\模板文档\\word文档\\本科\\【V42版】分院模板_非师范.doc");
//
//        HWPFDocument doc = new HWPFDocument(fis);
//        // 获取文档范围
//        Range range = doc.getRange();
//        //存放大纲名称
//        String OutlineName = null;
//
//        // 遍历所有段落
//        for (int i = 0; i < range.numParagraphs(); i++) {
//            Paragraph paragraph = range.getParagraph(i);
//            String style = paragraph.getStyleIndex() == -1 ? null : doc.getStyleSheet().getStyleDescription(paragraph.getStyleIndex()).getName();
//            String text = paragraph.text();
//
//            if (style != null) {
//                if (style.equals("MycosCaption")||style.equals("MycosHeader4")) {
//                    OutlineName = text;
////                    如果该段落为脚注内容则将前一个大纲内容和脚注内容一起放入txt文档文档中
//                } else if (style.equals("MycosDescription")&&i!=0&&!text.contains("EMBED")) {
//                    System.out.println("****************************************");
//                    System.out.println(OutlineName);
//                    System.out.println("正文:" + text);
//                }
//            }
//            // 关闭流
//            doc.close();
//            fis.close();
//        }
//
//    }


    public static void main(String[] args) throws Exception {
        FileInputStream fis = new FileInputStream("C:\\Users\\EDY\\Desktop\\模版信息\\中期文档\\模板文档\\word文档\\本科\\【V42版】分院模板_非师范.doc");
        HWPFDocument document = new HWPFDocument(fis);
        //存放大纲名称
        String OutlineName = null;
        for (int i = 0; i < document.getRange().numParagraphs(); i++) {
            Paragraph paragraph = document.getRange().getParagraph(i);
            String style = paragraph.getStyleIndex() == -1 ? null : document.getStyleSheet().getStyleDescription(paragraph.getStyleIndex()).getName();
            String text = paragraph.text();
            if (style != null) {
                if (style.equals("MycosCaption") || style.equals("MycosHeader4")) {
                    OutlineName = text;
//                    如果该段落为正文内容则将前一个大纲内容和正文内容一起放入txt文档文档中
                } else if (style.equals("MycosDescription") && i != 0 && !text.contains("EMBED")) {
                    StringBuilder fontWeight = new StringBuilder();
                    System.out.println(OutlineName);
//                    System.out.println("##正文:" + text);
                    for (int j = 0; j < paragraph.numCharacterRuns(); j++) {
                        CharacterRun run = paragraph.getCharacterRun(j);
                        if (run.isBold()) {
                            fontWeight.append(run.text()+" ");
                            System.out.println("&&加粗文字：" + fontWeight);
                        }
                    }
                    System.out.println(fontWeight);
                    System.out.println(processingText(text,new StringBuilder(), fontWeight));

                }
            }
        }
        document.close();
        fis.close();
    }

    public static String processingText(String text,StringBuilder s, StringBuilder fontWeight) {
        text = "<p style=\\\"text-indent: 21pt; text-align: justify;\\\">" + text;
        System.out.println("111"+text);
        if (fontWeight != null && text.contains(fontWeight)) {
            text.replace(fontWeight, "<strong>" + fontWeight + "</strong><span style=\\\"color: rgb(0, 0, 0);\\\">");
        }


        s.append(text);
        System.out.println(s);
        return s.toString();
    }

    private static char modifyChar(char currentChar, char nextChar) {
        // 在这里添加您的修改逻辑，例如将当前字符和下一个字符拼接起来
        return currentChar;
    }
}